<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px">
          <el-col :span="5">
            <el-form-item label="客户名称">
              <el-select
                v-model="listQuery.fsiteNo"
                disabled
                clearable
                filterable
                class="filter-item"
                placeholder="请选择客户名称"
              >
                <el-option
                  v-for="item in fsiteNoBelongOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="日期说明">
              <el-input
                v-model.trim="listQuery.fholidayname"
                clearable
                class="filter-item"
                style="width: 200px"
                placeholder="请输入日期说明"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="日期">
              <el-date-picker
                v-model="datelist"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="handleFilter">查找</el-button>
              <el-button type="success" @click="handleCreate">添加</el-button>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <!-- 查询结果 -->
        <JNPF-table
          v-loading="listLoading"
          :data="list"
        >
          <el-table-column
            align="center"
            width="140"
            label="操作"
            class-name="small-padding"
          >
            <template slot-scope="scope">
              <el-button type="text" @click="handleUpdate(scope.row)"
                >编辑</el-button
              >
              <el-button type="text" @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>

          <el-table-column
            :index="indexMethod"
            align="center"
            label="序"
            type="index"
          />
          <el-table-column
            align="center"
            label="日期说明"
            prop="fholidayname"
          />
          <el-table-column
            :formatter="dateFormat"
            align="center"
            label="开始日期"
            prop="fbegdate"
          />
          <el-table-column
            :formatter="dateFormat"
            align="center"
            label="结束日期"
            prop="fenddate"
          />
          <el-table-column
            :formatter="datetimeFormatfoperator"
            align="center"
            label="操作员"
            prop="foperator"
          />
          <el-table-column
            :formatter="datetimeFormat"
            align="center"
            label="操作时间"
            prop="fupddate"
          />
        </JNPF-table>
        <paginations
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
        />
      </div>
    </div>
    <!-- 添加或修改对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
      width="70%"
    >
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px" ref="dataForm" :model="dataForm" :rules="rules">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="fsiteNo">
              <el-select
                v-model="dataForm.fsiteNo"
                disabled
                clearable
                filterable
                placeholder="请选择客户名称"
              >
                <el-option
                  v-for="item in fsiteNoBelongOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="el-form-item-small" label="类型" prop="ftype">
              <el-select
                v-model="dataForm.ftype"
                class="filter-item"
                placeholder="请选择类型"
              >
                <el-option
                  v-for="(key, value) in frangeMap"
                  :key="key"
                  :label="key"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item  label="开始日期" prop="fbegdate">
              <el-date-picker
                v-model="dataForm.fbegdate"
                type="date"
                clearable
                class="filter-item"
                placeholder="开始日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="fenddate">
              <el-date-picker
                v-model="dataForm.fenddate"
                type="date"
                clearable
                class="filter-item"
                placeholder="结束日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              class="text"
              label="日期说明"
              prop="fholidayname"
            >
              <el-input v-model="dataForm.fholidayname" />
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" @click="dialogFormVisible = false"
          >取消</el-button
        >
        <el-button type="primary" v-if="dialogStatus=='create'" @click="createData">确定</el-button>
        <el-button type="primary" v-else @click="updateData">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHoliday,
  createHoliday,
  updateHoliday,
  deleteHoliday,
} from "@/api/holiday";
import { fsiteNoBelongOptions } from "@/api/role";
import { getListArea } from "@/api/area";
import Pagination from "@/components/Pagination";
import moment from "moment";

const siteTypeMap = {
  0: "省",
  1: "市",
  2: "区",
  3: "学校",
};

const statusMap = {
  0: "禁用",
  1: "启用",
};

const frangeMap = {
  1: "假期",
  2: "工作日",
};

export default {
  name: "Holiday",
  components: { Pagination },
  filters: {
    siteTypeFilter(type) {
      return siteTypeMap[type];
    },
    frangeFilter(type) {
      return frangeMap[type];
    },
    statusFilter(type) {
      return statusMap[type];
    },
  },
  data() {
    return {
      getListuser: [], // 人员列表
      list: null,
      total: 0,
      fsiteNoBelongOptions: null,
      listLoading: true,
      datelist:[new Date(),new Date()],
      listQuery: {
        page: 1,
        limit: 20,
        fholidayname: undefined,
        begdate: undefined,
        enddate: undefined,
        sort: "fupddate",
        order: "desc",
      },
      frangeMap,
      siteTypeMap,
      statusMap,
      dataForm: {
        id: undefined,
        ftype: undefined,
        fholidayid: undefined,
        fholidayname: undefined,
        fsiteNo: undefined,
        fbegdate: undefined,
        fenddate: undefined,
        // fsiteState: undefined,
        fupddate: undefined,
        foperator: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      rules: {
        fholidayid: [
          { required: true, message: "日期编号不能为空", trigger: "blur" },
        ],
        fholidayname: [
          { required: true, message: "日期说明不能为空", trigger: "blur" },
        ],
        fsiteNo: [
          { required: true, message: "客户名称不能为空", trigger: "blur" },
        ],
        fbegdate: [
          { required: true, message: "开始日期不能为空", trigger: "blur" },
        ],
        fenddate: [
          { required: true, message: "结束日期不能为空", trigger: "blur" },
        ],
        ftype: [
          { required: true, message: "类型不能为空", trigger: "blur" },
        ] /*,
        fsiteState: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]*/,
      },
      permissionDialogFormVisible: false,
      systemPermissions: null,
      assignedPermissions: null,
      permissionForm: {
        roleId: undefined,
        permIds: [],
      },
    };
  },
  created() {
    this.listQuery.fsiteNo = this.$store.state.user.fsiteNo;
    this.getList();
    fsiteNoBelongOptions()
      .then((response) => {
        this.fsiteNoBelongOptions = response.data.data.list;
      })
      .catch(() => {
        this.fsiteNoBelongOptions = [];
      });
    // 人员弹出框
    getListArea({ flag: 13 })
      .then((response) => {
        this.getListuser = response.data.data.list;
      })
      .catch(() => {
        this.getListuser = [];
      });
  },
  methods: {
    formatStatus: function (row, column) {
      return row.fsiteState === 1 ? "启用" : "禁用";
    },
    datetimeFormatfoperator: function (row, column) {
      if (this.getListuser) {
        for (let i = 0; i < this.getListuser.length; i++) {
          if (row.foperator == this.getListuser[i].value) {
            return this.getListuser[i].label;
          }
        }
        return "";
      } else {
        return "";
      }
    },
    datetimeFormat(row, column) {
      const date = row[column.property];
      if (date === undefined || date === null) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    dateFormat(row, column) {
      const date = row[column.property];
      if (date === undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD");
    },
    formatSite: function (row, column) {
      for (let i = 0; i < this.fsiteNoBelongOptions.length; i++) {
        if (row.fsiteNo === this.fsiteNoBelongOptions[i].value) {
          return this.fsiteNoBelongOptions[i].label;
        }
      }
      return "";
    },
    getList() {
      this.listLoading = true;
      if(this.datelist!=null)
      {
        this.listQuery.begdate=this.datelist[0];
        this.listQuery.enddate=this.datelist[1];
      }
      else
      {
        this.listQuery.begdate="";
        this.listQuery.enddate="";
      }
      listHoliday(this.listQuery)
        .then((response) => {
          this.list = response.data.data.list;
          this.total = response.data.data.total;
          this.listLoading = false;
        })
        .catch(() => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
        });
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        fholidayid: undefined,
        fholidayname: undefined,
        fsiteNo: undefined,
        fbegdate: undefined,
        fenddate: undefined,
        // fsiteState: undefined,
        fupddate: undefined,
        ftype: undefined,
        foperator: undefined,
      };
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
      this.dataForm.ftype = "1";
      this.dataForm.fsiteNo = this.$store.state.user.fsiteNo;
      this.dataForm.userId = this.$store.state.user.id.toString();
      this.dataForm.fupddate = new Date();
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          createHoliday(this.dataForm)
            .then((response) => {
              this.list.unshift(response.data.data);
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "添加日期定义成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.msg,
              });
            });
        }
      });
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      // this.dataForm.fsiteState = row.fsiteState.toString()
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.dataForm.ftype = this.dataForm.ftype.toString();
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
      this.dataForm.userId = this.$store.state.user.id.toString();
      // console.log('11',moment(new Date()).format('YYYY-MM-DD HH:mm:ss'))
      this.dataForm.fupddate = new Date();
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          updateHoliday(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.fholidayid === this.dataForm.fholidayid) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新日期定义资料成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.msg,
              });
            });
        }
      });
    },
    handleDelete(row) {
      this.$confirm("是否要删除该节假日定义资料", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteHoliday(row)
          .then((response) => {
            this.$notify.success({
              title: "成功",
              message: "删除日期定义资料成功",
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          })
          .catch((response) => {
            this.$notify.error({
              title: "失败",
              message: response,
            });
          });
      });
    },
    indexMethod(index) {
      index = index + 1;
      return index;
    },
  },
};
</script>
